package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import handler.database;
import java.sql.*;



/**
 *
 * @author SONY VAIO
 */
public class RegistrasiServlet extends HttpServlet {
    
     @Override
    public void init() throws ServletException {
        super.init();
    }

     /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;

        String name = "";
        String username = "";
        String userpass = "";
        String email = "";
        
        String strQuery = "";
        Statement statement = null;
        ResultSet result = null;
        int Result;
        try {
            database db = new database();
            conn = db.getConnection();
            if (request.getParameter("username") != null && request.getParameter("username").length() > 0 && request.getParameter("password") != null && request.getParameter("password").length() > 0 
                    && request.getParameter("name") != null && request.getParameter("name").length() > 0 && request.getParameter("email")!= null && request.getParameter("email").length() > 0) {
                name = request.getParameter("name").toString();
                username = request.getParameter("username").toString();
                userpass = request.getParameter("password").toString();
                email = request.getParameter("email").toString();
                strQuery = "INSERT INTO user (username, password, email, role, max_number_room, ban, name) "
                        + "VALUES ('"+ username + "', '"+ userpass + "', '" + email + "' , 'user', 25, 0, '" + name + "')";
                System.out.println(strQuery);
                statement = conn.createStatement();
                Result = statement.executeUpdate(strQuery);
            } 
            conn.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            response.sendRedirect("index.jsp");
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            response.sendRedirect("index.jsp");
            out.close();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
